package com.demo.camel06.activemqconsume;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.log4j.PropertyConfigurator;

import javax.jms.ConnectionFactory;

public class ActiveMQConsumeDemo {

    public static void main(String[] args) throws Exception {

        // 日志
        PropertyConfigurator.configure("src/conf/log4j.properties");
        PropertyConfigurator.configureAndWatch("src/conf/log4j.properties", 1000);

        CamelContext context = new DefaultCamelContext();
        context.start();

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://192.168.99.100:61616");

        context.addComponent("activemq", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));


        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {

                from("activemq:queue:my_queue").to("seda:end");

            }
        });

        ConsumerTemplate consumerTemplate = context.createConsumerTemplate();
        String receiveBody = consumerTemplate.receiveBody("seda:end", String.class);

        System.out.println(receiveBody);

        // 通用没有具体业务意义的代码，只是为了保证主线程不退出
        synchronized (ActiveMQConsumeDemo.class) {
            ActiveMQConsumeDemo.class.wait();
        }
    }
}
